package com.kfyty.auto.host;

import com.kfyty.auto.host.service.HostService;
import com.kfyty.auto.host.utils.Cmd;
import com.kfyty.loveqq.framework.boot.K;
import com.kfyty.loveqq.framework.core.autoconfig.CommandLineRunner;
import com.kfyty.loveqq.framework.core.autoconfig.annotation.Autowired;
import com.kfyty.loveqq.framework.core.autoconfig.annotation.BootApplication;
import com.kfyty.loveqq.framework.core.autoconfig.annotation.Value;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@BootApplication
public class Main implements CommandLineRunner {
    @Value("${resolve.host}")
    private String host;

    @Value("${resolve.hosts:C:\\Windows\\System32\\drivers\\etc}")
    private String hostsPath;

    @Autowired
    private HostService hostService;

    @Override
    public void run(String... args) throws Exception {
        String ip = this.hostService.findAnyIp(this.host);

        this.hostService.writeToHosts(this.host, ip, this.hostsPath);

        Cmd.flushDNS();

        log.info("resolve ip succeed: " + this.host + " -> " + ip);
    }

    public static void main(String[] args) {
        System.setProperty("jdk.httpclient.allowRestrictedHeaders", "host");
        K.run(Main.class, args);
    }
}
